﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.ServiceModel.Syndication;
using System.IO;

namespace FeedReader.Shared.Extensions
{
    public static class MyExtensions
    {
        
        public static string getString(this SyndicationContent content,TextSyndicationContent summary)
        {
            try
            {
                StringBuilder builder = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(builder))
                {
                    content.WriteTo(writer, "content", "tempuri.org");
                    writer.Flush();
                    return builder.ToString();
                }
            }
            catch
            {
                if (summary.Text != null)
                {
                    return summary.Text;
                }
                else
                {
                    return "";
                }
            
            }
        }
        public static bool isEarlierThan(this DateTime thedate, DateTimeOffset thenewdate)
        {
            DateTime newdate;
            //newdate = DateTime.Parse(thenewdate.ToString());
            newdate = DateTime.Parse(thenewdate.ToString(), System.Globalization.CultureInfo.CreateSpecificCulture("en-GB").DateTimeFormat);
            return DateTime.Compare(thedate, newdate) < 0;
        }
        public static bool isLaterThan(this DateTimeOffset thedate, DateTimeOffset thenewdate)
        {
            return DateTimeOffset.Compare(thedate, thenewdate) > 0;
        }
        public static SyndicationFeed LoadXml(this SyndicationFeed document, String text)
        {        
            TextReader tr = new StringReader(text);
            XmlReader xmlReader = XmlReader.Create(tr);
            document= SyndicationFeed.Load(xmlReader);

            return document;
        }
        //this solution taken from http://stackoverflow.com/questions/79197/combining-two-syndicationfeeds/86322#86322
        public static String SaveAsString(this SyndicationFeed document)
        {
            StringBuilder builder = new StringBuilder();
            using (XmlWriter writer = XmlWriter.Create(builder))
            {
                document.SaveAsRss20(writer);
                writer.Flush();
                return builder.ToString();
            }
        }
        public static XElement GetXElement(this XmlNode node)
        {
            XDocument xDoc = new XDocument();
            using (XmlWriter xmlWriter = xDoc.CreateWriter())
                node.WriteTo(xmlWriter);
            return xDoc.Root;
        }

        public static XmlNode GetXmlNode(this XElement element)
        {
            using (XmlReader xmlReader = element.CreateReader())
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlReader);
                return xmlDoc.FirstChild;
            }
        }

        public static XDocument GetXDocument(this XmlDocument document)
        {
            XDocument xDoc = new XDocument();
            using (XmlWriter xmlWriter = xDoc.CreateWriter())
                document.WriteTo(xmlWriter);
            XmlDeclaration decl =
                document.ChildNodes.OfType<XmlDeclaration>().FirstOrDefault();
            if (decl != null)
                xDoc.Declaration = new XDeclaration(decl.Version, decl.Encoding,
                    decl.Standalone);
            return xDoc;
        }

        public static XmlDocument GetXmlDocument(this XDocument document)
        {
            using (XmlReader xmlReader = document.CreateReader())
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xmlReader);
                if (document.Declaration != null)
                {
                    XmlDeclaration dec = xmlDoc.CreateXmlDeclaration(document.Declaration.Version,
                        document.Declaration.Encoding, document.Declaration.Standalone);
                    xmlDoc.InsertBefore(dec, xmlDoc.FirstChild);
                }
                return xmlDoc;
            }
        }
    }
}
